package com.ruoyi.baseMain.controller;

import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.baseMain.domain.BaseCustom;
import com.ruoyi.baseMain.domain.vo.BaseAccountVo;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.dataMain.listener.BaseAccountImportListener;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.baseMain.domain.vo.BaseCustomVo;
import com.ruoyi.baseMain.domain.bo.BaseCustomBo;
import com.ruoyi.baseMain.service.IBaseCustomService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户信息
 *
 * @author linziheng
 * @date 2023-12-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseMain/custom")
public class BaseCustomController extends BaseController {

    private final IBaseCustomService iBaseCustomService;
    private final ISysUserService sysUserService;

    /**
     * 查询客户信息列表
     */
    @SaCheckPermission("baseMain:custom:list")
    @GetMapping("/list")
    public TableDataInfo<BaseCustomVo> list(BaseCustomBo bo, PageQuery pageQuery) {
//        sysUserService.selectUserById(LoginHelper.getLoginUser().getUserId())
        return iBaseCustomService.queryPageList(bo, pageQuery);
    }
    @Log(title = "开户合同上传", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/contractFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> contractFile(@RequestPart("file") MultipartFile file) throws Exception {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        Random random = new Random();
        int temp = random.nextInt(900000)+100000;
        String fileName =temp+"_"+ file.getOriginalFilename();
        String filePath = "/home/file/contractFile/"+fileName;
//        String filePath = "/Users/linziheng/Desktop/log/"+fileName;
        File dest = new File(filePath);
        file.transferTo(dest);
        return R.ok("上传成功",fileName);
    }
    @GetMapping("/getCustomList")
    public List<BaseCustomVo> getCustomList(BaseCustomBo bo) {
        return iBaseCustomService.queryList(bo);
    }

    @GetMapping("/getCustomNames")
    public Set<String> getCustomNames(BaseCustomBo bo) {
        List<BaseCustomVo> list = iBaseCustomService.queryList(bo);
        Set<String> set = new HashSet<>();

        for (BaseCustomVo baseCustomVo : list) {
            set.add(baseCustomVo.getNickName() + "|" + baseCustomVo.getSaleUserName());
        }

        return set;
    }
    /**
     * 导出客户信息列表
     */
    @SaCheckPermission("baseMain:custom:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseCustomBo bo, HttpServletResponse response) {
        List<BaseCustomVo> list = iBaseCustomService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户信息", BaseCustomVo.class, response);
    }

    /**
     * 获取客户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("baseMain:custom:query")
    @GetMapping("/{id}")
    public R<BaseCustomVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBaseCustomService.queryById(id));
    }

    /**
     * 新增客户信息
     */
    @SaCheckPermission("baseMain:custom:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R add(@Validated(AddGroup.class) @RequestBody BaseCustomBo bo) throws Exception {
//        return toAjax(1);
        return iBaseCustomService.insertByBo(bo);
    }

    /**
     * 修改客户信息
     */
    @SaCheckPermission("baseMain:custom:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseCustomBo bo) {
        return toAjax(iBaseCustomService.updateByBo(bo));
    }

    @GetMapping("/signUpdateDo/{id}")
    public R<BaseCustomVo> signUpdateDo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) throws Exception {

        iBaseCustomService.signUpdateDo(id);
        return R.ok();
    }
    /**
     * 删除客户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("baseMain:custom:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBaseCustomService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
